package 牛客_面试必刷.Month07.day11;

import java.util.HashMap;
import java.util.Stack;

public class 有效括号序列 {
    public boolean isValid (String s) {
        // write code here
        char[] arr = s.toCharArray();


        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < arr.length; i++) {

            if(arr[i] == '(') stack.push(')');
            else if(arr[i] == '[' ) stack.push(']');
            else if(arr[i] == '{') stack.push('}');
                //首先注意这个判断为空的条件是在前面的
            else if(stack.isEmpty() || arr[i] != stack.pop()) return false;
        }
        //这里是判断是否为空的
        return stack.isEmpty();
    }
}
